package org.feuyeux.dapr;

import io.dapr.client.DaprClient;
import io.dapr.client.DaprClientBuilder;
import io.dapr.client.domain.HttpExtension;
import lombok.extern.slf4j.Slf4j;

import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Component;

@Component
@Slf4j
public class GreetingDaprClient {
    private static final String SERVICE_APP_ID = "quarkus_svc";

    public static void hello() {
        HttpExtension httpExtension = HttpExtension.GET;
        Map<String, String> metadata = new HashMap<>();
        var returnType = byte[].class;
        byte[] response;
        String methodName;
        try (DaprClient client = (new DaprClientBuilder()).build()) {
            methodName = "hello/BingDwenDwen";
            response = client.invokeMethod(SERVICE_APP_ID, methodName, null, httpExtension, metadata, returnType)
                    .block();
            log.info(new String(response));
        } catch (Exception e) {
            log.error("", e);
        }
        try (DaprClient client = (new DaprClientBuilder()).build()) {
            methodName = "hello";
            response = client.invokeMethod(SERVICE_APP_ID, methodName, null, httpExtension, metadata, returnType)
                    .block();
            log.info(new String(response));
        } catch (Exception e) {
            log.error("", e);
        }
    }
}
